package com.example.cardgame_1

import android.annotation.SuppressLint
import android.content.res.Configuration
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.GridLayoutManager
import com.example.cardgame.model1.CardMatchingGame // 确保导入model1包

private val game: CardMatchingGame = CardMatchingGame(24)
class FourthActivity : AppCompatActivity() {
    // model1包的实例
    private lateinit var adapter: CardAdapter1

    @SuppressLint("MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_fourth)

        val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
        adapter = CardAdapter1(game)
        recyclerView.adapter = adapter

        // 以下逻辑完全不变
        val configure = resources.configuration
        recyclerView.layoutManager = if (configure.orientation == Configuration.ORIENTATION_PORTRAIT) {
            GridLayoutManager(this, 4)
        } else {
            GridLayoutManager(this, 6)
        }

        adapter.setOnClickListener {
            game.chooseCardAtIndex(it, 3)
            updateUI()
        }

        updateUI()

        val reset = findViewById<Button>(R.id.button_reset)
        reset.setOnClickListener {
            game.reset()
            updateUI()
        }

        val back = findViewById<Button>(R.id.button_back)
        back.setOnClickListener {
            finish()
        }
    }

    private fun updateUI(){
        adapter.notifyDataSetChanged()
        val score = findViewById<TextView>(R.id.textView)
        score.text = getString(R.string.score) + game.score.toString()
    }
}